package com.fmsh.nfcsensor.activity;

import android.nfc.Tag;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.fmsh.base.handler.BaseHandler;
import com.fmsh.base.handler.UIHandlerCallback;
import com.fmsh.base.ui.BaseNFCActivity;
import com.fmsh.base.ui.NFCReaderCallback;
import com.fmsh.base.utils.HintDialog;
import com.fmsh.base.utils.LogUtil;
import com.fmsh.base.utils.UIUtils;
import com.fmsh.nfcsensor.R;
import com.fmsh.nfcsensor.bean.SensorBean;
import com.fmsh.nfcsensor.tools.MyThread;
import com.fmsh.nfcsensor.tools.chart.LineChartManagerUtils;
import com.fmsh.nfcsensor.utils.Constants;
import com.github.mikephil.charting.charts.LineChart;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author wuyajiang
 * @date 2022/11/7
 */
public class ChartActivity extends BaseNFCActivity implements UIHandlerCallback {

    List<Float> dataList = new ArrayList<>();
    List<Float> resultList = new ArrayList<>();
    @BindView(R.id.line_chart)
    LineChart lineChart;
    @BindView(R.id.topbar)
    QMUITopBarLayout topbar;
    @BindView(R.id.tv_hint)
    TextView tvHint;
    @BindView(R.id.ll_hint)
    LinearLayout llHint;
    @BindView(R.id.spinner)
    Spinner spinner;
    @BindView(R.id.ll_switch)
    LinearLayout llSwitch;
    private LineChartManagerUtils chartManager;
    private QMUITipDialog mQmuiTipDialog;
    private SensorBean mSensorBean;
    private ArrayList<SensorBean> mSensorList;
    private String[] mSensorArray;

    private int currentSelect = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_chart;
    }

    @Override
    protected void initView() {
        setBackImage();
        Bundle extras = getIntent().getExtras();
        mSensorBean = extras.getParcelable(Constants.KEY_HANDLE_SEND_MSG);
        mSensorList = extras.getParcelableArrayList(Constants.KEY_HANDLE_SEND_MSG + 1);
        mSensorArray = new String[mSensorList.size()];

        for (int i = 0; i < mSensorList.size(); i++) {
            String type = UIUtils.getString(mContext, mSensorList.get(i).getTypeId());
            mSensorArray[i] = type + "传感器";
            if (type.equals(UIUtils.getString(mContext, mSensorBean.getTypeId()))) {
                currentSelect = i;
            }
        }
        selectChart();

        mQmuiTipDialog = HintDialog.loadingDialog(this, UIUtils.getString(mContext, R.string.loading));
        LogUtil.d(mQmuiTipDialog.toString());

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, mSensorArray);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setSelection(currentSelect);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                currentSelect = i;
                LogUtil.d("111111111111");
                mSensorBean = mSensorList.get(i);
//                llHint.setVisibility(View.VISIBLE);
//                llSwitch.setVisibility(View.GONE);
//                lineChart.setVisibility(View.GONE);
                String title = "";
                String label = "";
                resultList.clear();
                switch (mSensorBean.getType()) {
                    case Constants.T_VALUE:
//                        chartManager.setYAxis((float) (mSensorBean.getValue() + 10), (float) (mSensorBean.getValue() - 10), 5);
//                        title = UIUtils.getString(mContext, R.string.temperature_chart);
//                        label = UIUtils.getString(mContext, R.string.temperature_chart) + "/" + mSensorBean.getUnity();
                        initTemperatureChart(mSensorBean);
                        break;
                    case Constants.H_VALUE:
//                        title = UIUtils.getString(mContext, R.string.humidity_chart);
//                        label = UIUtils.getString(mContext, R.string.humidity_chart) + "/" + mSensorBean.getUnity();
                        initHumidityChart(mSensorBean);
                        break;
                    case Constants.P_VALUE:
//                        title = UIUtils.getString(mContext, R.string.pressure_chart);
//                        label = UIUtils.getString(mContext, R.string.pressure_chart) + "/" + mSensorBean.getUnity();
                        initPressureChart(mSensorBean);
                        break;
                    case Constants.B_VALUE:
//                        title = UIUtils.getString(mContext, R.string.brightness_chart);
//                        label = UIUtils.getString(mContext, R.string.brightness_chart) + "/" + mSensorBean.getUnity();
                        initBrightnessChart(mSensorBean);
                        break;
                }
//                setTitle(title);
//                chartManager = new LineChartManagerUtils(mContext, lineChart);
//                chartManager.initLegend(label);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    private void selectChart() {
        switch (mSensorBean.getType()) {
            case Constants.T_VALUE:
                initTemperatureChart(mSensorBean);
                break;
            case Constants.H_VALUE:
                initHumidityChart(mSensorBean);
                break;
            case Constants.P_VALUE:
                initPressureChart(mSensorBean);
                break;
            case Constants.B_VALUE:
                initBrightnessChart(mSensorBean);
                break;
        }
    }

    public void initTemperatureChart(SensorBean sensorBean) {
        setTitle(UIUtils.getString(mContext, R.string.temperature_chart));
        chartManager = new LineChartManagerUtils(mContext, lineChart);
        chartManager.setYAxis((float) (sensorBean.getValue() + 5), (float) (sensorBean.getValue() - 5), 5);
        chartManager.initLegend(UIUtils.getString(mContext, R.string.temperature_chart) + "/" + sensorBean.getUnity());
//        dataList.clear();
//        dataList.add((float) Float.parseFloat(String.format("%.2f", sensorBean.getValue())));
//        chartManager.addEntry(dataList);
    }

    public void initHumidityChart(SensorBean sensorBean) {
        setTitle(UIUtils.getString(mContext, R.string.humidity_chart));
        chartManager = new LineChartManagerUtils(mContext, lineChart);
        chartManager.setYAxis((float) (sensorBean.getValue() + 5), (float) (sensorBean.getValue() - 5), 5);
        chartManager.initLegend(UIUtils.getString(mContext, R.string.humidity_chart) + "/" + sensorBean.getUnity());

//        dataList.clear();
//        dataList.add((float) (int)Float.parseFloat(String.format("%.2f", sensorBean.getValue())));
//        chartManager.addEntry(dataList);
    }

    public void initPressureChart(SensorBean sensorBean) {
        setTitle(UIUtils.getString(mContext, R.string.pressure_chart));
        chartManager = new LineChartManagerUtils(mContext, lineChart);
        chartManager.setYAxis((float) (sensorBean.getValue() + 200), (float) (sensorBean.getValue() - 200), 5);
        chartManager.initLegend(UIUtils.getString(mContext, R.string.pressure_chart) + "/" + sensorBean.getUnity());
//
//        dataList.clear();
//        dataList.add((float) Float.parseFloat(String.format("%.2f", sensorBean.getValue())));
//        chartManager.addEntry(dataList);
    }

    public void initBrightnessChart(SensorBean sensorBean) {
        setTitle(UIUtils.getString(mContext, R.string.brightness_chart));
        chartManager = new LineChartManagerUtils(mContext, lineChart);
        chartManager.setYAxis((float) (sensorBean.getValue() + 500), (float) (sensorBean.getValue() - 500), 5);
        chartManager.initLegend(UIUtils.getString(mContext, R.string.brightness_chart) + "/" + sensorBean.getUnity());
//        chartManager.showNoDataText();
//        dataList.clear();
//        dataList.add((float) Float.parseFloat(String.format("%.2f", sensorBean.getValue())));
//        chartManager.addEntry(dataList);
    }

    @Override
    protected void initData() {


        setNfcReaderCallback(new NFCReaderCallback() {
            @Override
            public void onTagDiscovered(Tag tag) {
                mQmuiTipDialog.show();
                resultList.clear();
                Bundle bundle = new Bundle();
                bundle.putParcelable(Constants.KEY_NFC_TAG, mTag);
                bundle.putInt(Constants.KEY_NFCA_OPERATION_TYPE, 1);
                bundle.putInt(Constants.KEY_NFCA_OPERATION_TYPE + 1, mSensorBean.getType());
                Message message = new Message();
                message.what = 0;
                message.setData(bundle);
                MyThread.getInstance().getRunHandler().sendMessage(message);
                BaseHandler.getInstance().setUIHandlerCallback(ChartActivity.this::callback);
            }
        });
    }


    private ArrayList<Float> mTList = new ArrayList<>();
    private ArrayList<Float> mHList = new ArrayList<>();
    private ArrayList<Float> mBList = new ArrayList<>();
    private ArrayList<Float> mPList = new ArrayList<>();
    private void showChart(Bundle bundle, int what) {
        float aFloat = Float.parseFloat(bundle.getString(Constants.KEY_HANDLE_SEND_MSG));
        resultList.add(aFloat);
        if(resultList.size() == 1){
            switch (what) {
                case Constants.T_VALUE:
                    chartManager.setYAxis((float) (aFloat + 10), (float) (aFloat - 10), 5);
                    chartManager.addEntry(mTList);
                    mTList.clear();
                    break;
                case Constants.H_VALUE:
                    chartManager.setYAxis((float) (aFloat + 10), (float) (aFloat - 10), 5);
                    chartManager.addEntry(mHList);
                    mHList.clear();
                    break;
                case Constants.P_VALUE:
                    chartManager.setYAxis((float) (aFloat + 200), (float) (aFloat - 200), 5);
                    chartManager.addEntry(mPList,true);
                    mPList.clear();
                    break;
                case Constants.B_VALUE:
                    chartManager.setYAxis((float) (aFloat + 1000), (float) 0, 5);
                    chartManager.addEntry(mBList);
                    mBList.clear();
                    break;
            }
        }

        switch (what) {
            case Constants.T_VALUE:
                chartManager.addEntry(mTList);
                break;
            case Constants.H_VALUE:
                chartManager.addEntry(mHList);
                break;
            case Constants.P_VALUE:
                chartManager.addEntry(mPList,true);
                break;
            case Constants.B_VALUE:
                chartManager.addEntry(mBList);
                break;
        }
//        dataList.clear();
//        dataList.add(aFloat);
//        if(what == Constants.P_VALUE){
//            chartManager.addEntry(dataList,true);
//        }else {
//            chartManager.addEntry(dataList);
//
//        }
        if (resultList.size() == 1) {
            llHint.setVisibility(View.GONE);
            llSwitch.setVisibility(View.VISIBLE);
            lineChart.setVisibility(View.VISIBLE);
        }


    }

    @Override
    public void callback(int what, Bundle bundle) {
        mQmuiTipDialog.dismiss();
        switch (what) {
            case Constants.NFC_UNKNOWN_ERROR:
                if (resultList.isEmpty()) {
                    HintDialog.faileDialog(mContext, UIUtils.getString(mContext, R.string.nfc_communication_failure));
                }
                break;
            case Constants.T_VALUE:
                mTList.clear();
                mTList.add(Float.parseFloat(bundle.getString(Constants.KEY_HANDLE_SEND_MSG)));
                if(mSensorBean.getType() == Constants.T_VALUE){
                    showChart(bundle,what);
                }
                break;
            case Constants.H_VALUE:
                mHList.clear();
                mHList.add(Float.parseFloat(bundle.getString(Constants.KEY_HANDLE_SEND_MSG)));
                if(mSensorBean.getType() == Constants.H_VALUE){
                    showChart(bundle,what);
                }
                break;
            case Constants.P_VALUE:
                mPList.clear();
                mPList.add(Float.parseFloat(bundle.getString(Constants.KEY_HANDLE_SEND_MSG)));
                if(mSensorBean.getType() == Constants.P_VALUE){
                    showChart(bundle,what);
                }
                break;
            case Constants.B_VALUE:
                mBList.clear();
                mBList.add(Float.parseFloat(bundle.getString(Constants.KEY_HANDLE_SEND_MSG)));
                if(mSensorBean.getType() == Constants.B_VALUE){
                    showChart(bundle,what);
                }
                break;
            default:

                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

}
